package com.pur.zsms.timecontrol.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.pur.zsms.common.util.PubFun;
import com.pur.zsms.common.util.pub.FDate;
import com.pur.zsms.ordercustommanage.web.OrderCustControllor;
import com.pur.zsms.timecontrol.dao.ITimeControlDao;
import com.pur.zsms.timecontrol.po.TimeControlPo;
import com.pur.zsms.timecontrol.service.ITimeControlService;

@Service("timeControlService")
public class TimeControlServiceImpl implements ITimeControlService {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(OrderCustControllor.class);
	String msg = "";

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Resource(name = "timeControlDao")
	private ITimeControlDao timeControlDao;

	public boolean timeControl(long userId, String orderDate) throws Exception {

		List<TimeControlPo> list = timeControlDao.getTimeControlPoListByUserId(userId);

		if (list == null || list.size() == 0) {
			return false;
		} else if (list.size() > 1) {
			return false;
		} else {

			TimeControlPo timeControlPo = list.get(0);
			String nowDate = PubFun.getCurrentDate();
			String nowTime = PubFun.getCurrentTime();

			String nowDateTime = nowDate + " " + nowTime;
			String orderDateTime = nowDate + " " + timeControlPo.getTimeOpen();

			if (timeControlPo.getIsOpen() == 1 && !"".equals(timeControlPo.getWorksTimeFlag())) {// 开启标志
				if (timeControlPo.getWorksTimeFlag().equals("start")) {
					if (!PubFun.checkDateAndEqualFalse(nowDate, orderDate)) {// 校验日期
						if (nowDate.equals(orderDate)) {// 如果日期相等
							if (PubFun.isDateBefore(nowDateTime, orderDateTime)) {// 校验时刻
								msg = "msg001";
								return false;
							}
						}
					} else {
						return false;
					}
				} else if (timeControlPo.getWorksTimeFlag().equals("end")) {
					if (PubFun.checkDateAndEqualFalse(nowDate, orderDate)) {// 校验日期
						FDate fd = new FDate();

						Date d1 = fd.getDate(nowDate);
						Date d2 = fd.getDate(orderDate);

						if (PubFun.calInterval(d1, d2, "D") == 1) {// 如果日期相差1天
							if (!PubFun.isDateBefore(nowTime, timeControlPo.getTimeOpen())) {// 校验时刻
								msg = "msg001";
								return false;
							}

						}
					} else {
						return false;
					}
				}
			}
		}

		return true;
	}
}
